import api from "../api";
import { sleep } from '@/utils/common';
import { Loading } from 'element-ui';
const mixin = {
  data() {
    return {
      // loading
      loadingInstance: null,
      loginPages: ['/resetPassword', '/login', '/register', '/Register']
    };
  },
  methods: {
    go(path) {
      console.log("here we go ", path);
      this.$router.push(path);
    },
    goParam(param) {
      return this.$route.query[param];
    },
    goBack() {
      this.$router.go(-1);
    },
    goBackLogic(arr = []) {
      arr.push(this.$route.path);
      let routeList = this.$store.state.routeList;
      let rouLen = routeList.length;
      if (!rouLen) return this.go(-1);
      for (let i = 0; i < rouLen; i++) {
        let route = routeList[rouLen - i - 1];
        // 如果黑名单列表包含当前路由且为当前页面
        let flag = [];
        for (let j = 0; j < arr.length; j++) {
          if(route.indexOf(arr[j]) === -1) flag.push(true);
          else flag.push(false);
        }
        flag = Array.from(new Set(flag));
        if(flag.length === 1 && flag[0]) return this.go(route);
      }
      this.go(-1);
    },
    goBackNoLogin() {
      this.goBackLogic(this.loginPages);
    },
    sleep,
    setStorage(key, val) {
      if(!val) return;
      // key = key.replace(/\//g,'');
      let res = this.$store.state.pageStore;
      res[key] = val;
      this.$store.commit("update", { pageStore: res});
    },
    getStorage(key) {
      // key = key.replace(/\//g,'');
      return this.$store.state.pageStore[key];
    },
    $$confirm({type = 'info',text = '', title = '提示', btnConfirm = '确认', btnCancel = '取消'}) {
      return this.$confirm(text, title, {
        confirmButtonText: btnConfirm,
        cancelButtonText: btnCancel,
        type: type,
        offset: 300
      });
    },
    $$alert({text = '', title = '提示', btnConfirm = '确认'}) {
      return this.$alert(text, title, {
        confirmButtonText: btnConfirm
      });
    },
    $$loading(val = '加载中') {
      this.loadingInstance = Loading.service({
        text: val,
        background: "#222220"
      });
    },
    $$loadingClose() {
      this.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
        this.loadingInstance.close();
      });
    },
    $$warning(message) {
      this.$message({
        showClose: true,
        message,
        type: "warning",
        offset: 300
      });
    },
    $toast(val){
      this.$$warning(val);
    },
    $post: api.post,
    $get: api.get,
    $put: api.put,
    $del: api.del,
    $$error(message) {
      this.$message({
        showClose: true,
        message,
        type: "error",
        offset: 300
      });
    },
    $$success(message) {
      this.$message({
        showClose: true,
        message,
        type: "success",
        offset: 300
      });
    },
    goService() {
      let person = "游客";
      if (this.$store.state.personInfo && this.$store.state.personInfo.firstname) person = this.$store.state.personInfo.firstname;
      let url = `https://chat32.live800.com/live800/chatClient/chatbox.jsp?companyID=1430761&configID=108853&jid=6073339352&s=1&pagereferrer=${person}`;
      window.open(url, '_blank');
    },
    // topMenu 默认选中
    finalCheckedItem(val) {
      let res = this.$store.state.topMenuChecked;
      res.finalChecked = val;
      this.$store.commit("utopMenuChecked", res);
    },
    async getCartNum() {
      try {
        const res = await this.$get({
          url: "/rest/V1/minicart/total"
        });
        this.$store.commit("cartNum", res[0].total);
      } catch (e) {
        this.$toast(e.message);
      }
    },
    async getPersonInfo(){
      try {
        console.log('getPersonInfo');
          const res = await this.$get({
              url: '/rest/V1/customer/getCustomer'
          });
          this.person_info = res[0];
          this.$store.commit("update", {
            personInfo: res[0]
          });
      } catch (e) {
          this.$$error(e.message);
      }
    },
  },
  filters: {
    priceShow: (value = 0) => {
      let val = value.toString();
      let dot = val.indexOf('.');
      let length = val.length;
      if (dot === -1) return val + ".00";
      else if (dot === length - 1) return val + ".0";
      else if (dot <= length - 2) return Number(value).toFixed(2);
      else return val;
    },
    formatMoney: function(value = 0, len = 2) {
      return value ? value.toFixed(len) : value;
    },
    formatFixed2: function(value = 0) {
      return Math.round(value * 100) / 100;
    },
    formatFloor: function(value = 0) {
      let reg = /^-?\d+$/;
      if (reg.test(value)) return value;
      let s = JSON.stringify(value);
      return s.substring(0, s.indexOf(`.`) + 3);
    }
  }
};

export default mixin;
